import * as amqp from "amqplib";

// RabbitMQ 服务器的连接信息
const rabbitMQServer = "amqp://mingcai_mq:EUDNfpZRbhfjb8PZ@95.169.25.181";

// 定义日志级别
enum LogLevel {
    Error = "error",
    Warning = "warning",
    Info = "info",
}

// 生产者发送日志消息
async function sendLogMessage(level: LogLevel, message: string) {
    try {
        // 创建 RabbitMQ 连接
        const connection = await amqp.connect(rabbitMQServer);
        const channel = await connection.createChannel();

        // 定义交换机名称
        const exchangeName = "logs_direct";

        // 声明 Direct 交换机
        await channel.assertExchange(exchangeName, "direct", { durable: false });

        // 发送日志消息，使用不同的路由键表示不同的日志级别
        channel.publish(exchangeName, level, Buffer.from(message));
        console.log(`[x] Sent ${level} log: ${message}`);

        // 关闭连接
        await channel.close();
        await connection.close();
    } catch (error) {
        console.error("Error in sendLogMessage:", error);
    }
}

// 示例用法
// 发送错误日志
sendLogMessage(LogLevel.Error, "This is an error message");
sendLogMessage(LogLevel.Info, "This is an info message");
sendLogMessage(LogLevel.Warning, "This is an warning message");
